package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.http.HttpUtils;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {
    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.appSecret}")
    private String secret;

    /**
     * 登录临时凭证获取openId
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenId(String code) {
        //组装微信appid 和 secret请求微信接口获取accessToken
        Map<String, Object> params = getAppConfig();
        //拿到code去微信换取openid
        params.put("js_code", code);
        //http请求微信接口
        String result = HttpUtil.get(REQUEST_URL, params);
        //解析json
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //判断是否成功
        if (ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }
        String openid = jsonObject.getStr("openid");
        return openid;
    }

    /**
     * 通过临时手机凭证去获取电话号码
     *
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
        //组装微信appid 和 secret请求微信接口获取accessToken
        Map<String, Object> params = getAppConfig();
        //获取微信token
        String result = HttpUtil.get(TOKEN_URL, params);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //判断是否成功
        if (ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }
        String accessToken = jsonObject.getStr("access_token");
        //accessToken 去请求微信接口获取手机号
        String url = PHONE_REQUEST_URL + accessToken;
        //组装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", detailCode);
        String phoneResult = HttpUtil.post(url, JSONUtil.toJsonStr(map));
        JSONObject phoneJsonObject = JSONUtil.parseObj(phoneResult);
        if (phoneJsonObject.getInt("errcode") != 0) {
            throw new BaseException(phoneJsonObject.getStr("errmsg"));
        }
        String str = phoneJsonObject.getJSONObject("phone_info").getStr("phoneNumber");
        return str;
    }
    private Map<String, Object> getAppConfig() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        return paramMap;
    }
}
